﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

namespace School.SystemManage.WebPages.Research
{
    public partial class ResearchActivity :Base.PermissionBasePage
    {
        TechnologySchoolEntities db = new TechnologySchoolEntities();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                AspNetPager1.RecordCount = db.Article.Where(a => a.SubMenu.Categories == 6).Count();
                BindData();
            }
        }

        private void BindData()
        {
            SqlDataReader dataSource = DAL.PagerHelper.GetPagerData("Article", "ArticleID,Content,UpdateTime",
                "UpdateTime", AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex, "categories=6");
            lv.DataSource = dataSource;
            lv.DataBind();
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindData();
        }

        protected void lv_ItemCanceling(object sender, ListViewCancelEventArgs e)
        {
            if (e.CancelMode == ListViewCancelMode.CancelingEdit)
            {
                lv.EditIndex = -1;
                BindData();
            }
        }

        protected void lv_ItemDeleting(object sender, ListViewDeleteEventArgs e)
        {
            long aid = (long)lv.DataKeys[e.ItemIndex].Value;
            Article article = db.Article.FirstOrDefault(a => a.ArticleID == aid);
            db.DeleteObject(article);
            db.SaveChanges();

            BindData();
        }

        protected void lv_ItemEditing(object sender, ListViewEditEventArgs e)
        {
            lv.EditIndex = e.NewEditIndex;
            BindData();
        }

        protected void lv_ItemUpdating(object sender, ListViewUpdateEventArgs e)
        {
            try
            {
                long kid = (long)lv.DataKeys[e.ItemIndex].Value;
                Article article = db.Article.FirstOrDefault(a => a.ArticleID == kid);
                string content = ((TextBox)lv.EditItem.FindControl("txbContent")).Text.Trim();
                article.Content = content;
                if (db.SaveChanges() > 0)
                {
                    lv.EditIndex = -1;
                    BindData();
                }
                else
                {
                    lblMsg.Text = "编辑失败！";
                }
            }
            catch (Exception ex)
            {
                lblMsg.Text = "编辑发生异常，原因：" + ex.Message;
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                Article article = new Article();
                article.SubMenu = db.SubMenu.First(m => m.Categories == 6);
                article.Content = txbCon.Text.Trim();
                article.UpdateTime = DateTime.Now;
                article.IsChecked = 1;
                article.IsEnabled = true;

                db.AddToArticle(article);

                if (db.SaveChanges() > 0)
                {
                    lblMsg.Text = "添加成功!";
                    BindData();
                    txbCon.Text = string.Empty;
                }
                else
                {
                    lblMsg.Text = "添加失败!";
                }
            }
            catch (Exception ex)
            {
                lblMsg.Text = "添加发生异常，原因：" + ex.Message;
            }
        }

        #region 操作权限控制

        public override void SetEditDisable()
        {
            base.SetEditDisable();

            foreach (ListViewDataItem item in lv.Items)
            {
                if (item.ItemType == ListViewItemType.DataItem)
                {
                    LinkButton linkEdit = (LinkButton)item.FindControl("LinkButton1");
                    LinkButton linkDelete = (LinkButton)item.FindControl("LinkButton2");
                    linkEdit.Visible = false;
                    linkDelete.Visible = false;
                }
            }

            insertmodule.Visible = false;
        }

        public override void SetReviewDisable()
        {
            base.SetReviewDisable();
        }

        public override void SetViewDisable()
        {
            base.SetViewDisable();
        }

        #endregion
    }
}
